﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using AnyOffice.Model;
using System.IO;
using System.Data;
using AnyOffice.DAL;
using AnyOffice.Components;
using AnyOffice.Appcode;

namespace AnyOffice.Web.general.system.netshareGX
{
    public partial class diskshare_form : BasePage
    {
        public readonly DiskGXDao bll = new DiskGXDao();
        public readonly DiskTypeGXDao dtbll = new DiskTypeGXDao();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Binddisktype();
                if (!string.IsNullOrEmpty(Request.QueryString["DiskID"]))
                {
                    hdd_diskid.Value = Request.QueryString["DiskID"].ToString();
                    Model.DiskGX disk = bll.GetModel(base.Conn, Convert.ToInt64(hdd_diskid.Value));
                    BindInit(disk);
                }
            }
        }

        /// <summary>
        /// 保存事件
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (hdd_diskid.Value.Length > 0)
                {
                    Model.DiskGX ndi = bll.GetModel(base.Conn, Convert.ToInt64(hdd_diskid.Value));
                    string oldname = "";
                    string newname = "";
                    oldname = Public_foldGX.GetPath(ndi.Name, "", ndi.DiskTypeID.ToString());//获得盘符目录路径
                    newname = oldname.Replace(ndi.Name, txt_NDiskName.Text.Trim());
                    if (ndi.Name == txt_NDiskName.Text.Trim())
                    {
                        ndi = AddBind(ndi);
                        bll.Update(base.Conn, ndi);
                        MessageBox.ShowMsgScript(this, "保存成功！", "parent.menu_main.location.href='diskshare_list.aspx';");
                    }
                    else
                    {
                        if (Public_foldGX.RemoveFiles(oldname, newname, ndi.FileTypeID.ToString())) //修改物理文件夹名
                        {
                            ndi = AddBind(ndi);
                            bll.Update(base.Conn, ndi);
                            MessageBox.ShowMsgScript(this, "保存成功！", "parent.menu_main.location.href='diskshare_list.aspx';");

                        }
                        else
                        {
                            MessageBox.Show(this, "目录路径已存在，请重新输入！");
                            return;
                        }
                    }
                }
                else
                {
                    Model.DiskGX ndi = new Model.DiskGX();
                    ndi = AddBind(ndi);
                    string path = dtbll.GetModel(base.Conn, Convert.ToInt32(ndi.DiskTypeID)).BasePath + "\\" + txt_NDiskName.Text.Trim();
                    if (CreateFolder(path))//判断创建目录是否成功
                    {
                        bll.Add(base.Conn, ndi); //写入数据库
                        MessageBox.ShowMsgScript(this, "保存成功！", "parent.menu_main.location.href='diskshare_list.aspx';");
                    }
                    else
                    {
                        MessageBox.Show(this, "目录路径已存在，请重新输入！");
                    }
                }
            }
            catch (Exception ex) { MessageBox.Show(this, ex.Message); }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="dis"></param>
        public void BindInit(Model.DiskGX dis)
        {
            DataSet ds = bll.GetByNewsId(base.Conn, " DiskID =" + dis.DiskID);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                txt_MangerUser.InnerText = ds.Tables[0].Rows[0]["ToManger"].ToString() == string.Empty ? string.Empty : ds.Tables[0].Rows[0]["ToManger"].ToString() + ",";
                txt_ReadDept.InnerText = ds.Tables[0].Rows[0]["ToReadDept"].ToString() == string.Empty ? string.Empty : ds.Tables[0].Rows[0]["ToReadDept"].ToString() + ",";
                txt_RedRole.InnerText = ds.Tables[0].Rows[0]["ToReadRole"].ToString() == string.Empty ? string.Empty : ds.Tables[0].Rows[0]["ToReadRole"].ToString() + ",";
                txt_Reduser.InnerText = ds.Tables[0].Rows[0]["ToReadUser"].ToString() == string.Empty ? string.Empty : ds.Tables[0].Rows[0]["ToReadUser"].ToString() + ",";
                txt_loadDept.InnerText = ds.Tables[0].Rows[0]["ToDownDept"].ToString() == string.Empty ? string.Empty : ds.Tables[0].Rows[0]["ToDownDept"].ToString() + ",";
                txt_LaodRole.InnerText = ds.Tables[0].Rows[0]["ToDownRole"].ToString() == string.Empty ? string.Empty : ds.Tables[0].Rows[0]["ToDownRole"].ToString() + ",";
                txt_loadUser.InnerText = ds.Tables[0].Rows[0]["ToDowUser"].ToString() == string.Empty ? string.Empty : ds.Tables[0].Rows[0]["ToDowUser"].ToString() + ",";
            }
            txt_NDiskName.Text = dis.Name;
            MangerUser.Value = dis.WriteUser;
            ReadDept.Value = dis.ReadDept;
            ToPriv.Value = dis.ReadRole;
            ToUser.Value = dis.ReadUser;
            Loadrole.Value = dis.DownloadRole;
            loadDept.Value = dis.DownloadDept;
            loaduser.Value = dis.DownloadUser;
            ddl_DiskTypeID.SelectedValue = dis.DiskTypeID.ToString();
            txt_Seq.Text = dis.Seq.ToString();
        }

        /// <summary>
        /// 网络硬盘类型初始化
        /// </summary>
        public void Binddisktype()
        {
            ddl_DiskTypeID.DataSource = dtbll.GetList(base.Conn, " DiskTypeName <>'" + Public_foldGX.DiskTypeName2 + "'");
            ddl_DiskTypeID.DataBind();
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="dis"></param>
        public Model.DiskGX AddBind(Model.DiskGX dis)
        {
            dis.ParentDiskID = 0;
            if (ddl_DiskTypeID.SelectedValue != "-1")
            {
                dis.DiskTypeID = Convert.ToInt32(ddl_DiskTypeID.SelectedValue); 
            }

            if (txt_NDiskName.Text.Trim() != "")
            { dis.Name = txt_NDiskName.Text.Trim(); }

            if (MangerUser.Value.Trim() != "")
            { dis.WriteUser = MangerUser.Value; }
            if (txt_MangerUser.InnerText.Trim() == "")
            {
                dis.WriteUser = "";
            }
            if (ReadDept.Value.Trim() != "")
            { dis.ReadDept = ReadDept.Value; }
            if (txt_ReadDept.InnerText.Trim() == "")
            {
                dis.ReadDept = "";
            }
            if (ToPriv.Value.Trim() != "")
            { dis.ReadRole = ToPriv.Value; }
            if (txt_RedRole.InnerText.Trim() == "")
            {
                dis.ReadRole = "";
            }
            if (ToUser.Value.Trim() != "")
            { dis.ReadUser = ToUser.Value.Trim(); }
            if (txt_Reduser.InnerText.Trim() == "")
            {
                dis.ReadUser = "";
            }

            if (loadDept.Value.Trim() != "")
            { dis.DownloadDept = loadDept.Value.Trim(); }
            if (txt_loadDept.InnerText.Trim() == "")
            {
                dis.DownloadDept = "";
            }

            if (Loadrole.Value.Trim() != "")
            { dis.DownloadRole = Loadrole.Value.Trim(); }
            if (txt_LaodRole.InnerText.Trim() == "")
            {
                dis.DownloadRole = "";
            }
            if (loaduser.Value.Trim() != "")
            { dis.DownloadUser = loaduser.Value.Trim(); }
            if (txt_loadUser.InnerText.Trim() == "")
            {
                dis.DownloadUser = "";
            }
            dis.FileTypeID = 1;
            if (Session["UserID"] != null)
            {
                dis.UserID = Session["UserID"].ToString();
            }
            if (txt_Seq.Text.Trim() != "")
            {
                dis.Seq = int.Parse(txt_Seq.Text.Trim());
            }
            dis.UpdateTime = DateTime.Parse(DateTime.Now.ToShortDateString());
            return dis;
        }



        /// <summary>
        /// 创建物理文件夹
        /// </summary>
        /// <param name="FolderPathName"></param>
        protected bool CreateFolder(string FolderPathName)
        {
            if (FolderPathName.Trim().Length > 0)
            {
                try
                {
                    if (!Directory.Exists(FolderPathName))
                    {
                        Directory.CreateDirectory(FolderPathName);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message);
                }
            }
            return false;
        }
    }
}